﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using C.Entities;
using C.Unite;
using ChanExt;
using MySoft.Data;
using Newtonsoft.Json.Linq;

namespace ArtLand.OA.Project.SystemMgr
{
    public partial class DataType : CommonPageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {

                btnTypeAdd.OnClientClick = Window1.GetShowReference("", "辅助资料类别 - 添加");

                TreeBind();

            }
        }

        private void TreeBind()
        {
            Mgr_Tree.Nodes.Clear();
            List<Sys_BaseDataGroup> _groups = TbsDbSession.Default.From<Sys_BaseDataGroup>().Where(Sys_BaseDataGroup._.TopID == 0).ToList();
            Drop_TopGroupID.DataSource = _groups;
            Drop_TopGroupID.DataTextField = "GroupName";
            Drop_TopGroupID.DataValueField = "ID";
            Drop_TopGroupID.DataBind();
            Drop_TopGroupID.Items.Insert(0, new ListItem("--最上级--", "0"));

            foreach (Sys_BaseDataGroup item in _groups)
            {
                TreeNode tn = new TreeNode();
                tn.Text = item.GroupName;
                tn.NodeID = item.ID.ToString();
                tn.IconUrl = "~/icon/advancedsettings.png";
                ResolveSubTree(item.ID, tn);
                Mgr_Tree.Nodes.Add(tn);
            }

            //List<Tick_BaseDataType> typeList = TbsDbSession.Default.From<Tick_BaseDataType>().ToList();
            //Grid2.DataSource = typeList;
            //Grid2.DataBind();
        }
        private void ResolveSubTree(int id, TreeNode treeNode)
        {
            List<Sys_BaseDataGroup> items = TbsDbSession.Default.From<Sys_BaseDataGroup>().Where(Sys_BaseDataGroup._.TopID == id).ToList();
            //List<Tbs_ArticleSort> items = TbsDbSession.Default.From<Tbs_ArticleSort>().Where(Tbs_ArticleSort._.FatherID == id).OrderBy(Tbs_ArticleSort._.ShowNo.Asc).ToList();

            if (items.Count > 0)
            {
                treeNode.Expanded = true;
                foreach (Sys_BaseDataGroup row in items)
                {
                    TreeNode node = new TreeNode();
                    node.Text = row.GroupName;
                    node.NodeID = row.ID.ToString();
                    node.Expanded = true;
                    node.EnablePostBack = true;
                    node.Icon = Icon.Wrench;
                    treeNode.Nodes.Add(node);
                    ResolveSubTree(row.ID, node);
                }
            }
        }




        protected void treeMenu_NodeCommand(object sender, TreeCommandEventArgs e)
        {
            Sys_BaseDataGroup group = TbsDbSession.Default.From<Sys_BaseDataGroup>().Where(Sys_BaseDataGroup._.ID == int.Parse(e.NodeID)).ToFirst();
            //Grid1.Hidden = true;
            // Mt_Tree.Hidden = true;
            if (group.Mtype == 0)
            {
                Region2.IFrameUrl = "DataBaseGrid.aspx?gid=" + e.NodeID + "&gno=" + group.GroupNo;
                //Region2.Hidden = false;
                //Grid1.Hidden = false;
                //BindGrid(GetWhere());
            }
            else
            {
                Region2.IFrameUrl = "DataBaseTree.aspx?gid=" + e.NodeID + "&gno=" + group.GroupNo; ;
                //Region3.Hidden = false;

                // Mt_Tree.Hidden = false;
            }
        }



        protected void btn_GroupSave_Click(object sender, EventArgs e)
        {
            if (hidType.Text != "")
            {
                Sys_BaseDataGroup group = new Sys_BaseDataGroup();
                group.ID = int.Parse(hidType.Text);
                group.Attach();
                group.GroupName = txt_GroupName.Text;
                group.GroupNo = txt_GroupNo.Text;
                group.OrderID = int.Parse(txt_OrderID.Text);
                group.TopID = int.Parse(Drop_TopGroupID.SelectedValue);
                group.Mtype = int.Parse(Drp_Mtype.SelectedValue);
                TbsDbSession.Default.Update<Sys_BaseDataGroup>(group);
                PageContext.RegisterStartupScript(Window1.GetHideReference());
                TreeBind();
            }
            else
            {
                Sys_BaseDataGroup group = new Sys_BaseDataGroup();
                group.DeAttach();
                group.GroupName = txt_GroupName.Text;
                group.GroupNo = txt_GroupNo.Text;
                group.OrderID = int.Parse(txt_OrderID.Text);
                group.TopID = int.Parse(Drop_TopGroupID.SelectedValue);
                group.Mtype = int.Parse(Drp_Mtype.SelectedValue);
                TbsDbSession.Default.Insert<Sys_BaseDataGroup>(group);
                PageContext.RegisterStartupScript(Window1.GetHideReference());
                TreeBind();
            }
        }

        protected void btnTypeDel_Click(object sender, EventArgs e)
        {
            if (Mgr_Tree.SelectedNodeID != "")
            {
                int sid = int.Parse(Mgr_Tree.SelectedNodeID);
                TbsDbSession.Default.Delete<Sys_BaseDataGroup>(Sys_BaseDataGroup._.ID == sid);
                TreeBind();
            }
            else
                Alert.Show("您未选择任何项！");
        }

        protected void btnTypeEdit_Click(object sender, EventArgs e)
        {
            if (Mgr_Tree.SelectedNodeID != "")
            {
                int sid = int.Parse(Mgr_Tree.SelectedNodeID);
                hidType.Text = sid.ToString();
                Sys_BaseDataGroup group = TbsDbSession.Default.From<Sys_BaseDataGroup>().Where(Sys_BaseDataGroup._.ID == sid).ToFirst();
                txt_GroupName.Text = group.GroupName;
                txt_GroupNo.Text = group.GroupNo;
                txt_OrderID.Text = group.OrderID.Value.ToString();
                Drop_TopGroupID.SelectedValue = group.TopID.Value.ToString();
                Drp_Mtype.SelectedValue = group.Mtype.Value.ToString();
                PageContext.RegisterStartupScript(Window1.GetShowReference("", "辅助资料类别 - 编辑"));
            }
            else
                Alert.Show("您未选择任何项！");
        }

        protected void btnTypeAdd_Click(object sender, EventArgs e)
        {
            hidType.Text = "";
            txt_GroupName.Text = "";
            txt_GroupNo.Text = "";
            txt_OrderID.Text = "0";
            //if (Mgr_Tree.SelectedNodeID != "")
            //{
            //    Drop_TopGroupID.SelectedValue = Mgr_Tree.SelectedNodeID;
            //}
            PageContext.RegisterStartupScript(Window1.GetShowReference("", "辅助资料类别 - 新增"));
        }
    }
}